package infbmobile.pantallas;

import com.sun.lwuit.Button;
import com.sun.lwuit.Calendar;
import com.sun.lwuit.ComboBox;
import com.sun.lwuit.Command;
import com.sun.lwuit.Component;
import com.sun.lwuit.Dialog;
import com.sun.lwuit.Form;
import com.sun.lwuit.Label;
import com.sun.lwuit.animations.CommonTransitions;
import com.sun.lwuit.animations.Transition;
import com.sun.lwuit.animations.Transition3D;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import infbmobile.comunicaciones.ConexionPedirCita;
import infbmobile.main.InfBMobile;
import java.util.Date;
import java.util.Vector;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author inftel
 */
public class Pantallacalendario extends Form implements ActionListener {

    private InfBMobile midlet;
    private Object dobCal;
    private Label dobValue;
    private Command ok;
    private Calendar cldr;
    private Command Confirmar;
    private ComboBox box;
    private ConexionPedirCita con;
    private String fecha;

    public Pantallacalendario(InfBMobile midlet) {
        super("PedirCitas");
        this.midlet = midlet;
        ok = new Command("OK");
        Confirmar = new Command("Pedir Cita");
        Command[] cmds = new Command[1];
        cmds[0] = ok;

        Date d = new Date();

        cldr = new Calendar(d.getTime());
        //this.setTransitionInAnimator(Transition3D.createCube(1000, true));
        Dialog.show("Elije fecha", cldr, cmds);

        long seleccionado = cldr.getSelectedDay();
        Date tempDate = new Date(seleccionado);
        java.util.Calendar c = java.util.Calendar.getInstance();
        c.setTime(tempDate);
        fecha = c.get(java.util.Calendar.DAY_OF_MONTH) + "/" + (c.get(java.util.Calendar.MONTH) + 1) + "/" + c.get(java.util.Calendar.YEAR);

        con = new ConexionPedirCita(midlet,fecha, null);
        Vector horas = con.Sacarhoras();
        mostrarhoras(horas, fecha);



    }

    private void mostrarhoras(Vector v, String fecha) {

        Label lab = new Label("Elige hora");
        this.addComponent(lab);
        box = new ComboBox(v);

        this.addComponent(box);
        this.show();

        dobValue = new Label();
        dobValue.setText(" Fecha eligida el dia :" + fecha);

        //dobValue.repaint();
        this.addComponent(dobValue);
        this.show();

        this.addCommand(Confirmar);
        this.setCommandListener(this);


    //this.addComponent(cldr);
    //this.show();






    }

    public void actionPerformed(ActionEvent evento) {

        Object fuente = evento.getSource();
        if (fuente == Confirmar) {
            String plural = " ";
            String hora = box.getSelectedItem().toString();


            con = new ConexionPedirCita(midlet,fecha, hora);
            con.CrearCita();

            Form f = new Form("Cita pedia");
            //f.setTransitionInAnimator(Transition3D.createCube(1000, true));
            if (hora.charAt(1) != '1') {
                plural = "s ";

            }
            f.addCommand(ok);
            f.addComponent(new Label("Su cita ha sido reservada :"));
            f.addComponent(new Label("El Dia " + fecha));
            f.addComponent(new Label(" a la" + plural + hora));
            f.show();
            f.setCommandListener(this);
            f.setTransitionOutAnimator(CommonTransitions.createFade(1000));
            System.gc();


        } else if (fuente == ok) {
            this.deregisterAnimated(this.getTransitionInAnimator());
            midlet.mostrarPantallaMenuPrincipal();

        }




    }
}
